package com.yls.except.service.impl;

import com.yls.except.dao.BankDao;
import com.yls.except.dao.SystemDao;
import com.yls.except.dao.UserDao;
import com.yls.except.service.BankService;
import com.yls.except.util.StringIsempty;
import com.yls.except.util.ValueJude;
import com.yls.except.util.exception.IndentException;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * 银行卡serviceimpl
 * Created by xs on 2016/8/3.
 */
@Service
@Repository(value = "bankService")
public class BankServiceImpl implements BankService {
    @Resource(name = "bankDao")
    private BankDao bankDao;
    @Resource(name = "userDao")
    private UserDao userDao;
    @Resource(name = "systemDao")
    private SystemDao systemDao;

    @Override
    public int insertBindingBank(String uid, String name, String bankname, String bankaddress, String card, String phone, String reqip) {
        Map<String, Object> map = new HashMap<String, Object>();

        Map<String, Object> user = userDao.selectUserInfoByUid(uid);
        if(StringIsempty.isNullMap(user))
            return -13;
        if("0".equals(user.get("type") + ""))
            return -62;

        map.put("start", 0);
        map.put("count", 1);
        map.put("uid", uid);
        List<Map<String, Object>> banklist = bankDao.selectBankInfoByUid(map);
        Map<String, Object> bankinfo = null;
        if (!StringIsempty.isNullList(banklist))
            bankinfo = banklist.get(0);
        if (!StringIsempty.isNullMap(bankinfo) && "1".equals(bankinfo.get("isdisabled") + ""))
            return -22;  //已绑定
        if (!StringIsempty.isNullMap(bankinfo)) {
            bankinfo.put("bankname", bankname);
            bankinfo.put("address", bankaddress);
            bankinfo.put("banknum", card);
            bankinfo.put("binding_time", new Date().getTime());
            bankinfo.put("binding_ip", reqip);
            bankinfo.put("name", name);
            bankinfo.put("phone", phone);
            bankinfo.put("isdisabled", 1);
            return bankDao.updateBankInfo(bankinfo);
        } else {
            bankinfo = new HashMap<String, Object>();
            bankinfo.put("bankname", bankname);
            bankinfo.put("address", bankaddress);
            bankinfo.put("uid", uid);
            bankinfo.put("banknum", card);
            bankinfo.put("binding_time", new Date().getTime());
            bankinfo.put("binding_ip", reqip);
            bankinfo.put("name", name);
            bankinfo.put("phone", phone);
            bankinfo.put("isdisabled", 1);
            return bankDao.insertBankInfo(bankinfo);
        }
    }

    @Override
    public Map<String, Object> selectBandingBankInfoById(String bandingnum, String uid) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> user = userDao.selectUserInfoByUid(uid);
        if(StringIsempty.isNullMap(user))
            return null;
        if("0".equals(user.get("type") + "")) {
            map.put("tag", -62);
            map.put("message", "权限不足");
            return map;
        }

        Map<String, Object> bankinfo = bankDao.selectBankInfoById(bandingnum);
        if (!StringIsempty.isNullMap(bankinfo) && "1".equals(bankinfo.get("isdisabled") + "") && uid.equals(bankinfo.get("uid") + "")) {
//            String card = bankinfo.get("card") + "";
//            if (!StringIsempty.isallempty(card) && card.length() > 0)
//                card = card.replace(card.substring(0, card.length() - 4), "************");
//            bankinfo.put("card", card);
            bankinfo.put("tag", 1);
            return bankinfo;
        } else
            return null;
    }

    @Override
    public int updateBindingBank(String uid, String name, String bankname, String bankaddress, String card, String phone, String bandingnum) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> user = userDao.selectUserInfoByUid(uid);
        if(StringIsempty.isNullMap(user))
            return -13;
        if("0".equals(user.get("type") + ""))
            return -62;

        Map<String, Object> bankinfo = bankDao.selectBankInfoById(bandingnum);
        if (!StringIsempty.isNullMap(bankinfo) && "1".equals(bankinfo.get("isdisabled") + "") && uid.equals(bankinfo.get("uid") + "")) {
            bankinfo.put("bankname", bankname);
            bankinfo.put("address", bankaddress);
            bankinfo.put("banknum", card);
            bankinfo.put("name", name);
            bankinfo.put("phone", phone);
            return bankDao.updateBankInfo(bankinfo);
        } else
            return -13;
    }

    @Override
    public List<Object> selectBandingBankInfoByUid(String uid) {

        Map<String, Object> user = userDao.selectUserInfoByUid(uid);
        if(StringIsempty.isNullMap(user))
            return null;
        if("0".equals(user.get("type") + ""))
            return null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", 0);
        map.put("count", 1);
        map.put("uid", uid);
        List<Map<String, Object>> banklist = bankDao.selectBankInfoByUid(map);
        List<Object> list = new ArrayList<Object>();
        if (!StringIsempty.isNullList(banklist)) {
            for (int i = 0; i < banklist.size(); i++) {
                Map<String, Object> bankinfo = banklist.get(i);
                if (!StringIsempty.isNullMap(bankinfo) && "1".equals(bankinfo.get("isdisabled") + "") && uid.equals(bankinfo.get("uid") + ""))
                    list.add(bankinfo);
            }
        }
        return list;
    }

    @Override
    public int deleteBindingInfo(String uid, String bandingnum) {

        Map<String, Object> user = userDao.selectUserInfoByUid(uid);
        if(StringIsempty.isNullMap(user))
            return -13;
        if("0".equals(user.get("type") + ""))
            return -62;

        Map<String, Object> bankinfo = bankDao.selectBankInfoById(bandingnum);
        if (!StringIsempty.isNullMap(bankinfo) && "1".equals(bankinfo.get("isdisabled") + "") && uid.equals(bankinfo.get("uid") + "")) {
            bankinfo.put("isdisabled", 0);
            return bankDao.updateBankInfo(bankinfo);
        } else
            return -13;
    }

    @Override
    public int insertWithDrawApply(String uid, String bandingnum, String withdrawmoney, String reqip) {

        Map<String, Object> user = userDao.selectUserInfoByUid(uid);
        if(StringIsempty.isNullMap(user))
            return -13;
        if("0".equals(user.get("type") + ""))
            return -62;
        Map<String, Object> bankinfo = bankDao.selectBankInfoById(bandingnum);
        if (StringIsempty.isNullMap(bankinfo) || !"1".equals(bankinfo.get("isdisabled") + "") || !uid.equals(bankinfo.get("uid") + ""))
            return -50;
        Map<String, Object> useraccount = userDao.selectUserAccountByUid(uid);
        if (StringIsempty.isNullMap(useraccount) || Long.parseLong(useraccount.get("sparemoney") + "") < (long)(Float.parseFloat(withdrawmoney) * 100))
            return -51;
        useraccount.put("sparemoney", Long.parseLong(useraccount.get("sparemoney") + "") - (long)(Float.parseFloat(withdrawmoney) * 100));
        int tag = userDao.updateUserAccount(useraccount);
        if(tag <= 0)
            throw new IndentException();
        Map<String, Object> withdraw = new HashMap<String, Object>();
        withdraw.put("withdraw_id", ValueJude.getUuid());
        withdraw.put("uid", uid);
        withdraw.put("money", (long)(Float.parseFloat(withdrawmoney) * 100));
        withdraw.put("type", 0);
        withdraw.put("bankname", bankinfo.get("bankname"));
        withdraw.put("banknum", bankinfo.get("banknum"));
        withdraw.put("bankaddress", bankinfo.get("address"));
        withdraw.put("withdraw_time", new Date().getTime());
        withdraw.put("withdraw_ip", reqip);
        withdraw.put("status", 0);
        withdraw.put("isdisabled", 1);
        return bankDao.insertWithDrawApply(withdraw);
    }
}
